package com.hopedove.workshop.utils

import android.util.Log
import android.widget.ArrayAdapter
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Spinner
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableInt
import com.hopedove.workshop.R


@BindingAdapter("setEntries")
fun Spinner.setEntries(entries: List<String>?) {
    Log.d("QueryVM", "setEntries  $entries")
    entries?.let {
        val adapter = ArrayAdapter(context, R.layout.workshop_spinner_item, entries)
        adapter.setDropDownViewResource(R.layout.workshop_spinner_dropdown_item)
        this.adapter = adapter
    }
}

@BindingAdapter("setSelectionRecord")
fun RadioGroup.setSelectionRecord(record: ObservableInt?) {
    record?.let {
        setOnCheckedChangeListener { group, checkedId ->
            group.findViewById<RadioButton>(checkedId)?.let { button ->
                (button.tag as? Int)?.let { data -> record.set(data) }
            }
        }
    }
}